﻿using System.Text;
using System.Web;

namespace Weibo.Common.Pager
{
    /// <summary>
    /// 简单HTML分页
    /// </summary>
    public class PageToolBar
    {
        private int pageCount;
        private int pageIndex;
        private string pagehtml;
        private int pagesize = 25;
        private int recordcount;

        public PageToolBar()
        {
            pageIndex = 0;
            recordcount = 0;
            pagesize = 25;
            pageCount = 0;
            pagehtml = "";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageSize">分页大小</param>
        /// <param name="recordCount">记录总数</param>
        public PageToolBar(int pageSize, int recordCount)
        {
            pagesize = pageSize;
            recordcount = recordCount;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="recordCount">记录总数</param>
        public PageToolBar(int recordCount)
        {
            recordcount = recordCount;
        }

        ///// <summary>
        ///// 当前页
        ///// </summary>
        public int PageIndex
        {
            get
            {
                if (pageIndex == 0)
                {
                    GetToolBar();
                }
                return pageIndex;
            }
        }

        /// <summary>
        /// 分页大小
        /// </summary>
        public int PageSize
        {
            get { return pagesize; }
            set { pagesize = value; }
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int RecordCount
        {
            get { return recordcount; }
            set { recordcount = value; }
        }

        /// <summary>
        /// 分页HTML
        /// </summary>
        public string PageHtml
        {
            get
            {
                //if (string.IsNullOrEmpty(pagehtml))
                //{
                GetToolBar();
                //} 

                return pagehtml;
            }
        }

        //}
        //public int pageCount
        //{
        //    get;
        //    set;
        //}
        /// <summary>
        /// 获取分布Html
        /// </summary>
        /// <param name="pageSize">分页大小</param>
        /// <param name="recordCount">记录总数</param>
        /// <returns></returns>
        private void GetToolBar(int pageSize, int recordCount)
        {
            pagesize = pageSize;
            recordcount = recordCount;
            GetToolBar();
        }


        private void setindex()
        {
            pageCount = recordcount / pagesize;
            if (recordcount % pagesize != 0)
            {
                pageCount++;
            }


            if (PageRequest.GetInt("page") < 1)
            {
                pageIndex = 1;
            }
            else
            {
                pageIndex = PageRequest.GetInt("page");
                if (pageIndex > pageCount)
                {
                    pageIndex = pageCount;
                }
            }
        }


        private void setIndex()
        {
            string _url = HttpContext.Current.Request.RawUrl;
            _url = _url.Substring(_url.IndexOf("?") + 1);
            pageCount = recordcount / pagesize;
            if (recordcount % pagesize != 0)
            {
                pageCount++;
            }
            if (_url.IndexOf("page") == -1)
            {
                pageIndex = 1;
            }
            else if (_url.IndexOf("&") != -1)
            {
                string[] arr = _url.Split('&');
                foreach (string str in arr)
                {
                    if (str.IndexOf("page=") != -1)
                    {
                        int.TryParse(str.Substring(5), out pageIndex);
                        break;
                    }
                }
            }
            else
            {
                int.TryParse(_url.Substring(5), out pageIndex);
            }
            pageIndex = pageIndex == 0 ? 1 : pageIndex;
            //if (pageIndex > pageCount)
            //{
            //    pageIndex = pageCount;
            //}

            //if (PageRequest.GetInt("page") < 1)
            //{
            //    pageIndex = 1;
            //}
            //else
            //{
            //    pageIndex = PageRequest.GetInt("page");
            //    if (pageIndex > pageCount)
            //    {
            //        pageIndex = pageCount;
            //    }
            //}
        }

        /// <summary>
        /// 获取分页
        /// </summary>
        /// <returns></returns>
        private void GetToolBar()
        {
            //pageCount = recordcount / pagesize;
            //if (recordcount % pagesize != 0)
            //{
            //    pageCount++;
            //}


            setIndex();
            var tool = new StringBuilder();
            string parastring = "";
            //GetPara();

            if (recordcount <= 0)
            {
                pagehtml =
                    string.Format("\n<div class=\"page_toolbar\"><div class=\"quotes\">\n对不起，没有找到任何记录</div></div>\n");
                return;
            }
            if (HttpContext.Current.Request.QueryString.Count > 0)
            {
                for (int i = 0; i <= HttpContext.Current.Request.QueryString.Count - 1; i++)
                {
                    if (HttpContext.Current.Request.QueryString.GetKey(i) == "page")
                        continue;
                    parastring =
                        string.Concat(new object[]
                                          {
                                              parastring, "&", HttpContext.Current.Request.QueryString.GetKey(i), "=",
                                              HttpContext.Current.Request.QueryString[i]
                                          });
                }
            }
            if (pageIndex <= 1)
            {
                tool.AppendLine("   <span class=\"disabled\"><<</span>");
                tool.AppendLine("   <span class=\"disabled\"><</span>");
            }
            else
            {
                tool.AppendLine("   <a href=?page=1" + parastring + "><<</a>");
                tool.AppendLine(
                    string.Concat(new object[] { "   <a href=?page=", pageIndex - 1, parastring, "><</a>" }));
            }
            for (int i = ((pageIndex - 4) > 0) ? (pageIndex - 4) : 1;
                 i <= (((pageIndex + 4) < pageCount) ? (pageIndex + 4) : pageCount);
                 i++)
            {
                if (i == pageIndex)
                {
                    tool.AppendLine("<span class=\"current\">" + i + "</span>");
                }
                else
                {
                    tool.AppendLine(
                        string.Concat(new object[] { "<a href=\"?page=", i, parastring, "\">", i, "</a>" }));
                }
            }
            if ((pageIndex + 5) < pageCount)
            {
                tool.AppendLine("...");
                tool.AppendLine(
                    string.Concat(new object[] { "<a href=\"?page=", pageCount, parastring, "\">", pageCount, "</a>" }));
            }
            if (pageIndex >= pageCount)
            {
                tool.AppendLine("   <span class=\"disabled\">></span>");
                tool.AppendLine("   <span class=\"disabled\">>></span>");
            }
            else
            {
                tool.AppendLine(
                    string.Concat(new object[] { "   <a href=?page=", pageIndex + 1, parastring, ">></a>" }));
                tool.AppendLine(string.Concat(new object[] { "   <a href=?page=", pageCount, parastring, ">>></a>" }));
            }

            pagehtml = string.Format("\n<div class=\"page_toolbar\"><div class=\"page_info\"><div class=\"quotes\">\n{0}  <span class=\"disabled\">当前第{1}页，共{2}页</span></div></div></div>\n", tool.ToString(), pageIndex, pageCount);
        }

        //private void GetPara()
        //{
        //    if (this.pageIndex < 1)
        //    {
        //        this.pageIndex = TLGame.WebPage.PageRequest.GetInt("pageindex");
        //    }
        //    if (this.pageIndex < 1)
        //    {
        //        this.pageIndex = 1;
        //    }
        //    this.para = PageRequest.GetPara();
        //}
    }
}